--[[
	游戏服调度机器人接口
--]]

local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local base = require "base"
local filename = "gang28scheduler.lua"

local gamename = "gang28node"

local gang28scheduler = {
	CMD = {},
    is_run = false,       --是否运行
}

local PLAYER_MAX = 10

---返回机器人的数量
function gang28scheduler.CMD.getrobotnum()

end

function gang28scheduler.CMD.change_schedule_state(newstate)
    -- gang28scheduler.is_run = newstate
end

function gang28scheduler.CMD.robotentertable(table_index, robotnum, robotconf)
	local status, result = skynet.pcall(skynet.call, ".schedulemgr", "lua", "request_robot_enter", gamename, table_index, robotnum, robotconf)
end

function gang28scheduler.CMD.robotentermatchtable(match_type, match_level, robotnum, enter_score)
    --入场，底注随机3~5倍
    local min = 7500    -- 1 * enter_score 
    local max = 8500    -- 5 * enter_score
    local moneyvalue = base.get_random(min, max)
    local robotconf = {diamond = 0, coin = 0, logicname = "gang28"}
    if match_type == "diamond" then
        robotconf.diamond = moneyvalue
    elseif match_type == "coin" then
        robotconf.coin = moneyvalue
    end
    skynet.call(".schedulemgr", "lua", "request_robot_match", gamename, match_type, match_level, robotnum, robotconf)
end

function gang28scheduler.request_createtable(base_score)
    --创建自建桌 底注范围(50-100金豆) 人数条件(非满人桌不足2桌)
    local table_conf = {
        table_type = 1,	--钻石场
		game_type = 1,
		game_room_type = 3,
		max_player_num = 5,
		is_public = 1,
		base_diamond = base_score or 50,
		enter_diamond = 500,
		leave_diamond = 500,
    }
    local rdata = {
        money = {diamond = 100000, coin = 100000},
        info = {rolename = '', logo = ''}
    }
    --建桌并进桌
    local status, result = skynet.pcall(cluster.call, gamename, ".router", "createtable", 101, rdata, {config = table_conf})
    -- filelog.debug("createtable ========>", status, result)
    if status and result and result.retcode == nil then
        gang28scheduler.CMD.robotentertable(result.table_index, 1, {
            diamond = math.random(7500, 8500),
            coin = math.random(7500, 8500),
            logicname = "gang28",
        })
    end
end

function gang28scheduler.check_alltableinfo()
    --桌子数量
    local ptable_nums = 0
    --[[]]
	local status, result = skynet.pcall(cluster.call, gamename, ".router", "get_alltableinfo")
    if status then
        --[[
        if result.pub_tableinfos and result.all_pubnum < PLAYER_MAX then    --只处理公开桌
            for table_index, tableinfo in pairs(result.pub_tableinfos) do
                if tableinfo.table_type == 1 and tableinfo.game_type == 1 then
                    local base_score = tableinfo.base_score or 0
                    if base_score <= 90 then
                        local robot_num = tableinfo.robot_num
                        if tableinfo.cur_player > 0 and tableinfo.cur_player < 2 and robot_num == 0 then   --小于3人，概率加入机器人
                            local min = 50
                            local player_num = tableinfo.cur_player
                            if player_num == 1 and robot_num == 0 then          --1个真人
                                min = 80
                            elseif player_num == 1 and robot_num == 1 then      --1个机器人
                                min = 50
                            elseif player_num == 2 and robot_num == 0 then      --2个真人
                                min = 30
                            elseif player_num == 2 and robot_num == 1 then      --1个真人，1个机器人
                                min = 25
                            elseif player_num == 2 and robot_num == 2 then      --2个机器人
                                min = 20
                            end
                            if math.random( 100 ) <= min then
                                gang28scheduler.CMD.robotentertable(table_index, 1, {
                                    diamond = math.random(7500, 8500),
                                    coin = math.random(7500, 8500),
                                    logicname = "gang28",
                                })
                            end
                        end
                    end
                    ptable_nums = ptable_nums + 1
                end
            end
        end
        --]]
        if result.match_infos then
            for match_level, match_info in pairs(result.match_infos) do
                -- if match_info.game_type == 1 and match_info.base_score <= 90 then
                    if match_info.cur_robot_num < 1 and match_info.waiting_player <= 1 and match_info.waiting_robot == 0 then
                        if match_info.unfull_table_num == 0 then
                            gang28scheduler.CMD.robotentermatchtable("diamond", match_level, 1, match_info.enter_score)
                        end
                    end
                -- end
            end
        end
        --[[
        if result.coin_match_infos then
            for match_level, match_info in pairs(result.coin_match_infos) do
                if match_info.game_type == 1 then
                    if match_info.cur_robot_num < 1 then
                        gang28scheduler.CMD.robotentermatchtable("coin", match_level, 1, match_info.enter_score)
                    end
                end
            end
        end
        --]]
    end

    ----------------
    ---建桌调度

    --保证1个50底注桌，1个100底注桌
    -- if ptable_nums == 0 then
    --     gang28scheduler.request_createtable(score)
    -- end
end

function gang28scheduler.monitor()
	-- body
	skynet.fork(function()
        while true do
        	skynet.sleep(6 * 100)
            ----定时拉取游戏所有桌子信息
            if gang28scheduler.is_run then
                pcall(gang28scheduler.check_alltableinfo)
            end
        end
    end)
end

skynet.start(function()
	skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = gang28scheduler.CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
    gang28scheduler.monitor()
end)
